package com.yami.shop.rights.common.strategy.impl;

import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.yami.shop.bean.enums.RightsType;
import com.yami.shop.bean.event.AddRightsEvent;
import com.yami.shop.bean.event.TransferRightsEvent;
import com.yami.shop.mystery.common.model.ActivityPresellQualification;
import com.yami.shop.mystery.common.service.ActivityPresellQualificationServer;
import com.yami.shop.rights.common.dao.PersonRightsMapper;
import com.yami.shop.rights.common.dto.PersonRightsDto;
import com.yami.shop.rights.common.model.*;
import com.yami.shop.rights.common.service.ProdRightsPreemptionService;
import com.yami.shop.rights.common.service.RightsRecordService;
import com.yami.shop.rights.common.strategy.PersonRightsStrategy;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Log4j2
@Service
public class PresellPersonRightStrateayImpl implements PersonRightsStrategy {

    @Autowired
    private ProdRightsPreemptionService prodRightsPreemptionService;
    @Autowired
    private PersonRightsMapper personRightsMapper;
    @Autowired
    private RightsRecordService rightsRecordService;
    @Autowired
    private ActivityPresellQualificationServer activityPresellQualificationServer;

    @Override
    public String rightsType() {
        return RightsType.PREEMPTION_RIGHTS.value();
    }

    @Override
    public void savePersonRightsData(ProdRights dto) {
        if (ObjectUtil.isNotEmpty(dto.getPreemptions())) {
            // 修改原有绑定信息
            LambdaUpdateWrapper<ProdRightsPreemption> prodRightsPreemptionLambdaUpdateWrapper = new LambdaUpdateWrapper<>();
            prodRightsPreemptionLambdaUpdateWrapper.eq(ProdRightsPreemption::getRightsId, dto.getId());
            prodRightsPreemptionLambdaUpdateWrapper.set(ProdRightsPreemption::getStatus, -1);
            prodRightsPreemptionService.update(prodRightsPreemptionLambdaUpdateWrapper);
            // 新增新的绑定信息
            dto.getPreemptions().forEach(s -> {
                s.setRightsId(dto.getId());
                s.setStatus(1);
            });
            prodRightsPreemptionService.saveBatch(dto.getPreemptions());
        }
    }

    @Override
    public Long getSurplusTimes(long id) {
        Long presellerSurplusTimes = personRightsMapper.getPresellerSurplusTimes(id);
        return ObjectUtil.isNotEmpty(presellerSurplusTimes) ? presellerSurplusTimes : 0L;
    }

    @Override
    public void addPersonRights(ProdRights prodRights, AddRightsEvent event, long personRightsId) {
        log.info("增加优先购权益" + event.getUserId());
        if (prodRights.getPreemptionType() == 1) {
            QueryWrapper<ProdRightsPreemption> prodRightsPreemptionQueryWrapper = new QueryWrapper<>();
            prodRightsPreemptionQueryWrapper.eq("rights_id", prodRights.getId());
            prodRightsPreemptionQueryWrapper.eq("status", 1);
            List<ProdRightsPreemption> list = prodRightsPreemptionService.list(prodRightsPreemptionQueryWrapper);
            list.forEach(s -> {
                // 新增购买资格
                ActivityPresellQualification activityPresellQualification = new ActivityPresellQualification();
                activityPresellQualification.setUserId(event.getUserId());
                activityPresellQualification.setTimes(s.getQuota());
                activityPresellQualification.setSourceType(1L);
                activityPresellQualification.setSourceId(personRightsId + "");
                activityPresellQualification.setEffectiveTime(prodRights.getExchangeEndTime());
                activityPresellQualification.setType(prodRights.getPreemptionType());
                if ("1".equals(s.getBlindType())) {
                    activityPresellQualification.setProdId(s.getBlindId());
                } else {
                    activityPresellQualification.setBoxId(s.getBlindId());
                }
                activityPresellQualificationServer.save(activityPresellQualification);
            });
        } else {
            ActivityPresellQualification activityPresellQualification = new ActivityPresellQualification();
            activityPresellQualification.setUserId(event.getUserId());
            activityPresellQualification.setTimes(prodRights.getDrawCount());
            activityPresellQualification.setSourceType(1L);
            activityPresellQualification.setSourceId(personRightsId + "");
            activityPresellQualification.setEffectiveTime(prodRights.getExchangeEndTime());
            activityPresellQualification.setType(prodRights.getPreemptionType());
            activityPresellQualificationServer.save(activityPresellQualification);
        }

    }

    @Override
    public List<PersonRightsDto> verificationRights(JSONObject qrCodeData) {
        List<PersonRightsDto> personRightsDtos = new ArrayList<>();
        String qualificationId = qrCodeData.getString("qualificationId");
        ActivityPresellQualification qualificationDto = activityPresellQualificationServer.getById(qualificationId);
        if (qualificationDto.getSourceType() == 1) {
            PersonRightsDto record = new PersonRightsDto();
            PersonRights personRights = personRightsMapper.selectById(qualificationDto.getSourceId());
            record.setRightsId(personRights.getRightsId());
            record.setTimes(1);
            record.setUserId(qrCodeData.getString("userId"));
            record.setHolderUserId(qrCodeData.getString("userId"));
            record.setRemark(qrCodeData.getString("orderItemId"));
            record.setStatus("2");
            record.setId(personRights.getId());
            // 如果剩余次数为0 当前权益置为已使用
            if(qualificationDto.getTimes() == 0){
                record.setStatusUpdate(1);
                qualificationDto.setStatus(1);
                activityPresellQualificationServer.updateById(qualificationDto);
            }
            personRightsDtos.add(record);
        }
        return personRightsDtos;
    }

    @Override
    public void updatePersonRights(Long rights,String personRightsId, String userId, String toUserId) {
        // 修改购买资格持有人
        personRightsMapper.updatePersonRights(personRightsId, userId, toUserId);

    }

    @Override
    public void cancelOrder(Long orderNumber) {
        activityPresellQualificationServer.addTimesBySourceId(orderNumber,1);
    }

    @Override
    public void updateRightsEffective(List<Long> personRightsId) {
        LambdaUpdateWrapper<ActivityPresellQualification> activityPresellQualificationLambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        activityPresellQualificationLambdaUpdateWrapper.in(ActivityPresellQualification::getSourceId, personRightsId);
        activityPresellQualificationLambdaUpdateWrapper.eq(ActivityPresellQualification::getSourceType, 2);
        activityPresellQualificationLambdaUpdateWrapper.eq(ActivityPresellQualification::getStatus, 0);
        activityPresellQualificationLambdaUpdateWrapper.set(ActivityPresellQualification::getStatus, 3);
        activityPresellQualificationServer.update(activityPresellQualificationLambdaUpdateWrapper);
    }

    @Override
    public void rightsTransfer(TransferRightsEvent event) {

    }

}
